#pragma once
#include "base/Singleton.h"
#include "ShareData/ShareData.h"

class Socket;

class CNetwork
    : public IShareDataListener
{
private:
    CNetwork();
    ~CNetwork();

public:

    bool        Init(CShareData* pData);
    bool        InitClient(const std::string& strIpAddress);

    void        SendLine(std::string str);
    std::string GetLine();

    bool        OnDataChanged(DWORD dwStamp);

    unsigned    RunServThread();
    unsigned    RunClientThread();

    static unsigned __stdcall ServThreadFunc(void* p);
    static unsigned __stdcall ClientThreadFunc(void* p);

private:
    bool        _uninit();

private:
    CShareData* m_pData;
    std::string m_strSend;
    std::string m_strRecv;

    HANDLE      m_hThreadServ;
    UINT        m_nThreadServId;
    HANDLE      m_hThreadClient;
    UINT        m_nThreadClientId;

    DWORD       m_dwStamp;

    bool        m_bExit;

    Socket*     m_pServer;
    Socket*     m_pClient;

    std::string m_strIpAddress;

    volatile    LONG    m_bSend;

    static const INT UM_SEND_STRING;

    DECLARE_SINGLETON_CLASS(CNetwork)

private:
    DISALLOW_COPY_AND_ASSIGN(CNetwork)

};

typedef xBase::TxSingleton<CNetwork> Network;